﻿using System;
using System.ComponentModel;
using System.Data;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.Task.Template
{
    public abstract class Crud
    {
        public event FormEvent.FormEventHandler OnFetch;
        public event FormEvent.FormEventHandler OnFetchLists;
        public event FormEvent.FormEventHandler OnInsert;
        public event FormEvent.FormEventHandler OnUpdate;
        public event FormEvent.FormEventHandler OnDelete;
        public event FormEvent.FormEventHandler OnToggle;
        public event FormEvent.FormEventHandler OnError;
        public event FormEvent.FormEventHandler OnGetCount;
        protected bool IsSuccess;
        protected bool IsError;

        protected void TriggerFetch(FormEvent e)
        {
            if (OnFetch != null)
                OnFetch(this, e);
        }

        protected void TriggerFetchLists(FormEvent e)
        {
            if (OnFetchLists != null)
                OnFetchLists(this, e);
        }

        protected void TriggerInsert(FormEvent e)
        {
            if (OnInsert != null)
                OnInsert(this, e);
        }

        protected void TriggerUpdate(FormEvent e)
        {
            if (OnUpdate != null)
                OnUpdate(this, e);
        }

        protected void TriggerDelete(FormEvent e)
        {
            if (OnDelete != null)
                OnDelete(this, e);
        }

        protected void TriggerToggle(FormEvent e)
        {
            if (OnToggle != null)
                OnToggle(this, e);
        }

        protected void TriggerError(FormEvent e)
        {
            if (Config.DebugMode)
                if (OnError != null)
                    OnError(this, e);
        }

        protected void TriggerGetCount(FormEvent e)
        {
            if (OnGetCount != null)
                OnGetCount(this, e);
        }

        protected virtual void BgwFetchDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwFetchListsDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwInsertDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwUpdateDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwDeleteDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwToggleDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        protected virtual void BgwGetCountDoWork(object sender, DoWorkEventArgs e) { }

        protected virtual void BgwGetCountWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }

        public virtual void Fetch(UInt64 id)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwFetchDoWork;
                a.RunWorkerCompleted += BgFetchRunWorkerCompleted;
                a.RunWorkerAsync(id);
            }
        }

        public virtual void FetchLists()
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwFetchListsDoWork;
                a.RunWorkerCompleted += BgFetchListsRunWorkerCompleted;
                a.RunWorkerAsync();
            }
        }

        public virtual void FetchLists(object data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwFetchListsDoWork;
                a.RunWorkerCompleted += BgFetchListsRunWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        public virtual void Insert(DataTable data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwInsertDoWork;
                a.RunWorkerCompleted += BgInsertRunWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        public virtual void Update(DataTable data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwUpdateDoWork;
                a.RunWorkerCompleted += BgUpdateRunWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        public virtual void Delete(DataTable data)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwDeleteDoWork;
                a.RunWorkerCompleted += BgDeleteRunWorkerCompleted;
                a.RunWorkerAsync(data);
            }
        }

        public virtual void Toggle(UInt64 id, bool active)
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwToggleDoWork;
                a.RunWorkerCompleted += BgToggleRunWorkerCompleted;
                a.RunWorkerAsync(new object[] {id, active});
            }
        }

        public virtual void GetCount()
        {
            using (var a = new BackgroundWorker())
            {
                a.DoWork += BgwGetCountDoWork;
                a.RunWorkerCompleted += BgwGetCountWorkerCompleted;
                a.RunWorkerAsync();
            }
        }
    }
}